/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.awt.event.*;

import javax.swing.*;
import controllers.GUIController;
import hibernate.Activity;
import hibernate.Schedule;
import hibernate.ScheduledActivity;

import hibernate.User;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Owner
 */
public class AStudentSched extends javax.swing.JInternalFrame {
  private javax.swing.JPanel ButtonPanel;
    private javax.swing.JButton activitiesButton;
    private javax.swing.JScrollPane activitiesList;
    private javax.swing.JPanel activitiesPanel;
    private javax.swing.JButton addToSchedButton;
    private javax.swing.JPanel bkgrd;
    private javax.swing.JButton clearButton;
    private javax.swing.JLabel chooseDateLabel;
    private javax.swing.JComboBox comboBox;
    private javax.swing.JButton editStudentButton;
    private javax.swing.JButton homeButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel logo;
    private javax.swing.JButton logoutButton;
    private javax.swing.JButton myAccountButton;
    private javax.swing.JButton saveSchedButton;
    private javax.swing.JPanel schedulePanel;
    private javax.swing.JLabel studentNameLabel;
    private Calendar date;
    private String[] dates;
    private int dateDiff;
    private GUIController guiC;
    final AStudentSched obj = this;
    private User currentStudent;
    private JTextArea scheduleJList;
    private JScrollPane jscrollSched;
    private Activity currentActivity;
    private Schedule currentSchedule;
    private int currentIndex;
     private List<Schedule> schList;
     private List<Activity> activityList;
     private List<ScheduledActivity> schedActivityList;
     private DefaultListModel listModel = new DefaultListModel();

    
    public AStudentSched(GUIController m, User u) {
        
        guiC = m;
        currentStudent = u;
        dateDiff = 1;
        schList = new ArrayList();
        schedActivityList = new ArrayList();
        
        //addDatesToSchedules();
        currentStudent.setSchedules(schList);       //sets current student with that sch list
        
        //currentSchedule = schList.get(dateDiff);    //get today's schedule from schedList
         
        activityList = guiC.getActivityList();        //gets act from guic
        
//        currentSchedule.setActivities(schedActivityList);        //sets that current schedule with act list
        
        initComponents();
        
        addComboBoxActionListener(comboBox);
        addLogoutActionListener();
        addHomeActionListener();
        addMyAccountActionListener();
        addEditStudentActionListener();
        addClearActionListener();
        addActivitiesActionListener();
        add_ActivityListSelectionListener();
        addAddToSchedActionListener();
        addSaveActionListener();
        
        updateActivitiesList();
    }
    
    private void addSched(Schedule s){
            s.setUserId(guiC.currentUser.getUserId()); 
            s.setSchedCreatedOn(new Date());
            s.setSchedUdatedOn(new Date());
            schList.add(s); 
    }
   
     private void addActivitiesActionListener() {
        activitiesButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guiC.showGUI(new AAddActivity(guiC));
                obj.dispose();
            }
        });
    }
     
      private void addSaveActionListener() {  
          saveSchedButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                currentStudent.setSchedules(schList);
                guiC.saveDbData(currentSchedule);
                currentSchedule.setActivities(schedActivityList);
                guiC.saveActivities(schedActivityList);
                JOptionPane.showInternalMessageDialog(rootPane, "Schedule has been saved.");

            }
        });
    }

    // adds actionlistener to the logout button to log the admin out of their
    // account
    private void addLogoutActionListener() {
        logoutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // show login page/close application on logout
                guiC.showGUI(new ALogin(guiC));
                // close previous gui(AMyClass)
                obj.dispose();
            }
        });
    }

    //adds actionlistener to cancel button to go to the my account page
    private void addClearActionListener() {
        clearButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clearSchedule();
            }
        });
    }

    // adds actionlistener to myaccount button to go to the page My Class
    private void addMyAccountActionListener() {
        myAccountButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    guiC.showGUI(new AMyAccount(guiC));
                } catch (IOException ex) {
                    Logger.getLogger(AStudentSched.class.getName()).log(Level.SEVERE, null, ex);
                }
                obj.dispose();
            }
        });
    }

    //adds actionlistener to edit student button to go to the home page
    private void addEditStudentActionListener() {
        editStudentButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guiC.showGUI(new AEditStudent(guiC, currentStudent));
            }
        });
    }
    
    private void addAddToSchedActionListener() {
        addToSchedButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addActivityToSchedule(currentActivity);
                
            }
        });
    }

    //adds actionlistener to home button to go to the home page
    private void addHomeActionListener() {
        homeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guiC.showGUI(new AMyClass(guiC));
                obj.dispose();
            }
        });
    }

    //populates dates for the combobox/drop down menu
    public void populateDates() {
        //schList = currentStudent.getSchedules();
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, MMM dd, ''yy");
        String today = dateFormat.format(date.getTime());
        dates = new String[15];
        if(dateDiff+1>7){
            dateDiff=7;
        }
        dates[dateDiff+1] = today;
         //retrieves dates before index 7
        if (dateDiff != 0) {
            for (int i = (dateDiff+1); i >= 1 && i <= (dateDiff+1) ; i--) {
                if ((i - 1) != -1 && (i - 1) < dateDiff ) {
                    date.add(Calendar.DATE, -1);
                    String t = dateFormat.format(date.getTime());
                    int add = (dateDiff+1) - i;
                    dates[add] = t;
                }
            }
        }
        
         //retrives dates after today's index
        date = Calendar.getInstance();
        dates[dateDiff+1] = dateFormat.format(date.getTime());
        for (int i = 1; i >= 1 && i <= 7; i++) {
            date.add(Calendar.DATE, 1);
            String t = dateFormat.format(date.getTime());
            int a = (dateDiff+1) + i;
            dates[a] = t;
        }
     }

    //takes in the comboBox/dropdown menu and sets the 
    //actionlistener and it's properties
    //also makes schedules for the current day that the user has selected
    public void addComboBoxActionListener(JComboBox comboBox) {
        comboBox.setSelectedIndex(0); //sets default view to empty slot
        comboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JComboBox cb = ((JComboBox) e.getSource());      
                String d = (String)cb.getSelectedItem();
                currentIndex = cb.getSelectedIndex();
                if (findSchedule(d) == true) {
                    updateSchedule();
                    showActivities();
                } else {
                    Schedule s= new Schedule();
                    s.setStartDate(d);
                    addSched(s);
                    currentSchedule = s; //get the selected day's schedule
                    guiC.saveDbData(currentSchedule);
                    clearSchedule();
                    

                }
            }
        });
    }
    
    public boolean findSchedule(String date) {
        List<Schedule> s = currentStudent.getSchedules();
        if (currentIndex >= s.size()) {
            Schedule found = s.get(currentIndex);
            if (found.getStartDate().equals(date)) {
                currentSchedule = found;
                return true;
            }
        }

        return false;
    }
    
    
    public void updateSchedule(){
        clearSchedule();
        Schedule s = currentSchedule;
        schList.remove(currentSchedule);
        guiC.deleteDbData(currentSchedule);
        updateSchedInfo(s);
    }
    
    public void updateSchedInfo(Schedule s){
        s.setActivities(schedActivityList);
        currentSchedule = s;
        schList.add(currentIndex,s);
    }
    
    //prints list of activities to the text area if there are any
    private void showActivities(){
        List<ScheduledActivity> s= currentSchedule.getActivities();
        ScheduledActivity sa;
        for(Iterator i = s.iterator();i.hasNext();){
            sa = (ScheduledActivity) i.next();
            scheduleJList.setText(sa.getName() + "\n");
        }
    }
    
    
 //clears the scheduleJList   
private void clearSchedule(){
    scheduleJList.setText("");
}
        
    
private void add_ActivityListSelectionListener() {
            jList1.addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    int index = jList1.getSelectedIndex();
                    if (index > -1) {
                        Activity a = activityList.get(jList1.getSelectedIndex());
                        currentActivity = a;
                    }
                }
            });

        }
    
    
    private void addActivityToSchedule(Activity a){
        ScheduledActivity schedActivity = new ScheduledActivity();
        schedActivity.setSchedId(currentSchedule.getSchedId());
        schedActivity.setActivityId(a.getActivityId());
        schedActivity.setName(a.getName());
        schedActivity.setImage(a.getImage());
        schedActivity.setSound(a.getSound());
        schedActivity.setPhonetic(a.getPhonetic());
        schedActivityList.add(schedActivity);
        currentSchedule.setActivities(schedActivityList);
        scheduleJList.append( a.getName()+ "\n");
    }

    
    private void updateActivitiesList() {
        jList1.clearSelection();
        jList1.setModel(listModel);
        List tempList = new ArrayList();
        listModel.clear();
        Activity a;
        tempList = guiC.getActivityList();
        int ct = 0;
        for (Iterator i = tempList.iterator(); i.hasNext();) {
            a = (Activity) i.next();
            listModel.add(ct++, a.getName());
        }
        jList1.setModel(listModel);
        activityList = tempList;

    }
    
    @SuppressWarnings("unchecked")
    
    private void initComponents() {
        
        User u = currentStudent;
        Date d = new Date();
        long d1 = u.getCreateDateTime();
        int diff = (int)(d.getTime() - d1);
        dateDiff = (diff / (1000 * 60 * 60 * 24));
        date = Calendar.getInstance();// today's date
        jScrollPane1 = new javax.swing.JScrollPane();
        bkgrd = new javax.swing.JPanel();
        logo = new javax.swing.JLabel();
        ButtonPanel = new javax.swing.JPanel();
        homeButton = new javax.swing.JButton();
        myAccountButton = new javax.swing.JButton();
        logoutButton = new javax.swing.JButton();
        activitiesButton = new javax.swing.JButton();
        activitiesPanel = new javax.swing.JPanel();
        studentNameLabel = new javax.swing.JLabel(currentStudent.getFirstName() + " " + currentStudent.getLastName());
        chooseDateLabel = new javax.swing.JLabel();
        
        activitiesList = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        addToSchedButton = new javax.swing.JButton();
        schedulePanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        editStudentButton = new javax.swing.JButton();
        saveSchedButton = new javax.swing.JButton();
        clearButton = new javax.swing.JButton();
        jscrollSched = new javax.swing.JScrollPane();
        scheduleJList = new javax.swing.JTextArea();
        populateDates();
        comboBox = new JComboBox(dates);
        

         bkgrd.setBackground(new java.awt.Color(0, 102, 255));

        logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/OnPointlogo2-1.png"))); // NOI18N
       ButtonPanel.setOpaque(false);

        homeButton.setText("Home");
        homeButton.setOpaque(false);

        myAccountButton.setText("MyAccount");
        myAccountButton.setOpaque(false);

        logoutButton.setText("Logout");
        logoutButton.setOpaque(false);

        activitiesButton.setText("Activities");
        activitiesButton.setOpaque(false);

        javax.swing.GroupLayout ButtonPanelLayout = new javax.swing.GroupLayout(ButtonPanel);
        ButtonPanel.setLayout(ButtonPanelLayout);
        ButtonPanelLayout.setHorizontalGroup(
            ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(myAccountButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(homeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(logoutButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(activitiesButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        ButtonPanelLayout.setVerticalGroup(
            ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(homeButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(myAccountButton)
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(activitiesButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(logoutButton)
                .addContainerGap(152, Short.MAX_VALUE))
        );

        activitiesPanel.setBackground(new java.awt.Color(51, 153, 255));
        activitiesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Activities"));

        studentNameLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

        chooseDateLabel.setText("Choose the Date: ");

        activitiesList.setViewportView(jList1);

        addToSchedButton.setText("Add to Schedule");
        addToSchedButton.setOpaque(false);

        javax.swing.GroupLayout activitiesPanelLayout = new javax.swing.GroupLayout(activitiesPanel);
        activitiesPanel.setLayout(activitiesPanelLayout);
        activitiesPanelLayout.setHorizontalGroup(
            activitiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(activitiesPanelLayout.createSequentialGroup()
                .addComponent(studentNameLabel)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(activitiesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(activitiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(addToSchedButton)
                    .addGroup(activitiesPanelLayout.createSequentialGroup()
                        .addComponent(chooseDateLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(comboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(activitiesList, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap(50, Short.MAX_VALUE))
        );
        activitiesPanelLayout.setVerticalGroup(
            activitiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(activitiesPanelLayout.createSequentialGroup()
                .addComponent(studentNameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(activitiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chooseDateLabel)
                    .addComponent(comboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(activitiesList, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addToSchedButton)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        schedulePanel.setBackground(new java.awt.Color(51, 153, 255));
        schedulePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Schedule"));

        jLabel1.setBackground(new java.awt.Color(255, 0, 0));
        jLabel1.setText("***Add activities in the order they are to be completed.");

        editStudentButton.setText("Edit Student");
        editStudentButton.setOpaque(false);

        saveSchedButton.setText("Save Schedule");
        saveSchedButton.setOpaque(false);

        clearButton.setText("Clear Schedule");
        clearButton.setOpaque(false);

        scheduleJList.setColumns(20);
        scheduleJList.setRows(5);
        jscrollSched.setViewportView(scheduleJList);

        javax.swing.GroupLayout schedulePanelLayout = new javax.swing.GroupLayout(schedulePanel);
        schedulePanel.setLayout(schedulePanelLayout);
        schedulePanelLayout.setHorizontalGroup(
            schedulePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(schedulePanelLayout.createSequentialGroup()
                .addGroup(schedulePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(schedulePanelLayout.createSequentialGroup()
                        .addGap(91, 91, 91)
                        .addComponent(editStudentButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(saveSchedButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(clearButton))
                    .addGroup(schedulePanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addGroup(schedulePanelLayout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jscrollSched, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        schedulePanelLayout.setVerticalGroup(
            schedulePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(schedulePanelLayout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel1)
                .addGap(28, 28, 28)
                .addComponent(jscrollSched, javax.swing.GroupLayout.PREFERRED_SIZE, 318, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(schedulePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(editStudentButton)
                    .addComponent(saveSchedButton)
                    .addComponent(clearButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout bkgrdLayout = new javax.swing.GroupLayout(bkgrd);
        bkgrd.setLayout(bkgrdLayout);
        bkgrdLayout.setHorizontalGroup(
            bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(bkgrdLayout.createSequentialGroup()
                .addComponent(logo)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(bkgrdLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ButtonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(activitiesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(schedulePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(524, Short.MAX_VALUE))
        );
        bkgrdLayout.setVerticalGroup(
            bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(bkgrdLayout.createSequentialGroup()
                .addComponent(logo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(bkgrdLayout.createSequentialGroup()
                        .addComponent(ButtonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(203, 203, 203))
                    .addGroup(bkgrdLayout.createSequentialGroup()
                        .addGroup(bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(schedulePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(activitiesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap(62, Short.MAX_VALUE))))
        );

        jScrollPane1.setViewportView(bkgrd);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1319, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
        );

        pack();
    }// </editor-fold>

}
